package com.baidu.leetcode.sort;

/**
 * @author shilei
 * @create 2021-12-07 19:22
 * 选择排序演示
 * 选择排序 10万数据随机排序 3818 毫秒左右
 */
public class ChoiceSort {
    public static void main(String[] args) {
        // 演示选择排序
//        int[] arr = {99,2,44,1,77};
        int[] arr = new int[100000];
        for (int i = 0; i < 100000; i++) {
            arr[i] = (int) (Math.random() * 1000000);
        }
        long begin = System.currentTimeMillis();
        selectSort(arr);
        long end = System.currentTimeMillis();
        System.out.println(end-begin);

    }

    public static void selectSort(int[] arr) {
        if (arr.length <= 0) {
            return;
        }
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            int min = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (min > arr[j]) {
                    //说明当前min 不是最小值 进行交换
                    min = arr[j];
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                arr[minIndex] = arr[i];
                arr[i] = min;
            }
//            System.out.println("第"+(i+1)+"轮后~~~");
//            System.out.println(Arrays.toString(arr));

        }
    }
}
